package com.sec.android.app.myfiles.module.local.recent;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;

import com.sec.android.app.myfiles.module.abstraction.AbsProviderImp;
import com.sec.android.app.myfiles.module.abstraction.FileRecord;
import com.sec.android.app.myfiles.module.download.DownloadFileRecord;
import com.sec.android.app.myfiles.module.download.providerapps.AbsSupportAppProviderImp;
import com.sec.android.app.myfiles.provider.DbTableInfo;

/**
 * Created by daesu83.kim on 2015-09-11.
 */
public class RecentDownloadProviderImp extends AbsSupportAppProviderImp {
    private int mFileIdIndex;
    private int mPathIndex;
    private int mNameIndex;
    private int mSizeIndex;
    private int mDateIndex;
    private int mFileTypeIndex;
    private int mExtIndex;
    private int mSourceIndex;
    private int mDescIndex;
    private int mStatusIndex;
    private int mMimeTypeIndex;
    private int mDownloadByIndex;

    private String DUPLICATED_ITEM_SELECTION;

    public RecentDownloadProviderImp(Context context, AbsProviderImp provider, SQLiteDatabase db) {
        super(context, provider, db);
    }

    @Override
    protected Cursor getCursor() {
        Cursor ret = null;
        int nMaxID = getProviderMaxIdEntry();
        DbTableInfo tableInfo = DbTableInfo.getInstance(FileRecord.StorageType.Downloads);
        if (tableInfo != null) {
            ret = mContentResolver.query(Uri.parse(tableInfo.getUri()), null, getSelection(nMaxID), null, null);
        }
        return ret;
    }

    private String getSelection(int nMaxID) {
        StringBuilder selection = new StringBuilder();
        selection.append(mTableInfo.getColumnName(DbTableInfo.COLUMN_ID.DOWNLOAD_ID))
                .append('>')
                .append(nMaxID)
                .append(" AND ")
                .append(mTableInfo.getColumnName(DbTableInfo.COLUMN_ID.DOWNLOAD_BY))
                .append('=')
                .append(DownloadFileRecord.DownloadType.DOWNLOAD_BY_BROWSER.ordinal());
        return selection.toString();
    }

    @Override
    protected ContentValues getContentValues(Cursor c) {
        ContentValues ret = new ContentValues();
        ret.put(COL_DownloadId, c.getInt(mFileIdIndex));
        ret.put(COL_Path, c.getString(mPathIndex));
        ret.put(COL_Name, c.getString(mNameIndex));
        ret.put(COL_Size, c.getLong(mSizeIndex));
        ret.put(COL_Date, c.getLong(mDateIndex));
        ret.put(COL_FileType, c.getInt(mFileTypeIndex));
        ret.put(COL_Ext, c.getString(mExtIndex));
        ret.put(COL_MimeType, c.getString(mMimeTypeIndex));
        ret.put(COL_Source, c.getString(mSourceIndex));
        ret.put(COL_Description, c.getString(mDescIndex));
        ret.put(COL_Status, c.getInt(mStatusIndex));
        ret.put(COL_DownloadBy, c.getInt(mDownloadByIndex));
        removeDuplicatedItem(ret);
        return ret;
    }

    private void removeDuplicatedItem(ContentValues ret) {
        String path = ret.getAsString(COL_Path);
        String name = ret.getAsString(COL_Name);
        if (path != null && name != null) {
            String[] whereArgs = new String[]{path, name};
            try (Cursor c = mDB.query(mTableInfo.getTableName(), null, DUPLICATED_ITEM_SELECTION,
                    whereArgs, null, null, null)) {
                if (c != null) {
                    if (c.getCount() > 0) {
                        mDB.delete(mTableInfo.getTableName(), DUPLICATED_ITEM_SELECTION, whereArgs);
                    }
                    c.close();
                }
            }
        }
    }

    @Override
    protected void setColumnIndex(Cursor cursor) {
        mFileIdIndex = cursor.getColumnIndexOrThrow(mTableInfo.getColumnName(DbTableInfo.COLUMN_ID.DOWNLOAD_ID));
        mPathIndex = cursor.getColumnIndex(mTableInfo.getColumnName(DbTableInfo.COLUMN_ID.PATH));
        mNameIndex = cursor.getColumnIndex(mTableInfo.getColumnName(DbTableInfo.COLUMN_ID.NAME));
        mSizeIndex = cursor.getColumnIndex(mTableInfo.getColumnName(DbTableInfo.COLUMN_ID.SIZE));
        mDateIndex = cursor.getColumnIndex(mTableInfo.getColumnName(DbTableInfo.COLUMN_ID.DATE));
        mFileTypeIndex = cursor.getColumnIndex(mTableInfo.getColumnName(DbTableInfo.COLUMN_ID.FILE_TYPE));
        mExtIndex = cursor.getColumnIndex(mTableInfo.getColumnName(DbTableInfo.COLUMN_ID.EXT));
        mMimeTypeIndex = cursor.getColumnIndex(mTableInfo.getColumnName(DbTableInfo.COLUMN_ID.MIME_TYPE));
        mSourceIndex = cursor.getColumnIndex(mTableInfo.getColumnName(DbTableInfo.COLUMN_ID.SOURCE));
        mDescIndex = mMimeTypeIndex = cursor.getColumnIndex(mTableInfo.getColumnName(DbTableInfo.COLUMN_ID.DESCRIPTION));
        mStatusIndex = cursor.getColumnIndex(mTableInfo.getColumnName(DbTableInfo.COLUMN_ID.STATUS));
        mDownloadByIndex = mMimeTypeIndex = cursor.getColumnIndex(mTableInfo.getColumnName(DbTableInfo.COLUMN_ID.DOWNLOAD_BY));
    }

    @Override
    protected boolean needClearUnnecessaryData() {
        return true;
    }

    @Override
    protected DownloadFileRecord.DownloadType getDownloadType() {
        return DownloadFileRecord.DownloadType.DOWNLOAD_BY_BROWSER;
    }

    @Override
    protected void setColumnName() {
        super.setColumnName();
        StringBuilder selection = new StringBuilder();
        selection.append(COL_Path)
                .append("=? AND ")
                .append(COL_Name)
                .append("=?");
        DUPLICATED_ITEM_SELECTION = selection.toString();
    }

    public void refreshAlone(Uri uri) {
        _refresh(uri);
    }
}